<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2007/03/28 11:14:11 $)
// $RCSfile: functions_api.php,v $ : $Revision: 1.1 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* ###############################################
* BEGIN CONSTANTS
* ###############################################
*/

define("ERROR_LOGINFAILURE", 100);
define("ERROR_INVALIDSESSION", 101);
define("ERROR_INVALIDDATA", 102);
define("ERROR_DBFAILURE", 103);

/**
* ###############################################
* END CONSTANTS
* ###############################################
*/


/**
* Example: http_post("www.fat.com", 80, "/weightloss.pl", array("name" => "obese bob", "age" => "20"));
*/
function http_post($server, $port, $url, $vars) {

	$user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)";


	$urlencoded = "";
	while (list($key,$value) = each($vars))
		$urlencoded.= urlencode($key) . "=" . urlencode($value) . "&";
	$urlencoded = substr($urlencoded,0,-1);	

	$content_length = strlen($urlencoded);

	$headers = "POST $url HTTP/1.1\nAccept: */*
Accept-Language: en-au
Content-Type: application/x-www-form-urlencoded
User-Agent: $user_agent
Host: $server
Connection: Keep-Alive
Cache-Control: no-cache
Content-Length: $content_length

";
	
	$fp = fsockopen($server, $port, $errno, $errstr);
	if (!$fp) {
		return false;
	}

	fputs($fp, $headers);
	fputs($fp, $urlencoded);
	
	$ret = "";
	while (!feof($fp))
		$ret.= fgets($fp, 1024);
        
	fclose($fp);
	
	return $ret;
}

/**
* Generates a random text, useful for usernames and passwords
*/
function generateRandom()
{
  $salt = "abcdefghijklmnopqrstuvwxyz"; 
      $i = 0; 
      while ($i <= 12) { 
			srand((double)microtime()*1000000); 
            $num = rand() % 26; 
            $tmp = substr($salt, $num, 1); 
            $pass = $pass . $tmp; 
            $i++; 
      } 
      return $pass; 
}

/**
* Displays an API Error
*/
function doAPIError($errorno, $errortxt)
{
	global $xml;

	$xml->addParentTag("api");
		$xml->addTag("status", "0");
		$xml->addTag("errorno", $errorno);
		$xml->addTag("errortxt", $errortxt);
	$xml->endTag("api");
	$xml->echoXML();
	exit;
}

/**
* Gets the API Status from a parsed array
*/
function getAPIStatus($apiarray)
{
	$api = &$apiarray["api"][0]["children"];

	if ($api["status"][0]["values"][0] == "1")
	{
		return true;
	} else {
		return false;
	}
}

/**
* Gets the API Data from an XML Parsed Array
*/
function getAPIData($apiarray)
{
	$data = array();

	$api = &$apiarray["api"][0]["children"];

	if ($api["status"][0]["values"][0] == "1")
	{
		$apidata = $api["data"][0]["children"];
		foreach ($apidata as $key=>$val)
		{
			$data[$key] = $apidata[$key][0]["values"][0];
		}
		return $data;
	} else {
		return false;
	}
}

/**
* Converts an array to API xml
*/
function arrayToAPI($arraydata)
{
	global $xml;

	$xml->buildXML();
	$xml->addParentTag("api");
		$xml->addTag("status", "1");
		$xml->addParentTag("data");
			foreach ($arraydata as $key=>$val)
			{
				$xml->addTag($key, $val);
			}
		$xml->endTag("data");
	$xml->endTag("api");

	$xml->echoXML();
}
?>